package com.ruizhong.centercontroll.util;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InputExtractor {

    public static List<String> extract(String patternStr,String input,Integer count){
        Pattern pattern=Pattern.compile(patternStr);
        if (pattern == null|| StringUtils.isEmpty(input)){
            return Collections.EMPTY_LIST;
        }
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            List<String>  resList = new ArrayList<>();
            for (Integer i = 1; i <= count; i++) {
                resList.add(matcher.group(i));
            }
            // 如果匹配成功，提取并返回匹配组中的值（即 # 符号后的部分）
            return resList;
        } else {
            // 如果匹配失败，打印错误消息并返回null
            System.err.println("Input string does not match the expected format: " + input);
            return Collections.EMPTY_LIST;
        }
    }
}
